<template>
  <el-card>
    <div slot="header" class="header">
      <span>商品分类</span>
      <el-button type="primary" size="small" @click="addgoodsort">添加分类</el-button>
    </div>
    <div class="content">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="width">
        </el-table-column>
        <el-table-column label="分类名称" width="cateName">
          <template slot-scope="scope">
            <el-input
              v-if="scope.row.canEdit"
              v-model="scope.row.cateName"
            ></el-input>
            <span v-else>{{ scope.row.cateName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-switch
              :disabled="scope.row.canEdit ? false : true"
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch> </template
        ></el-table-column>
        <el-table-column width="width">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.canEdit ? 'success' : 'primary'"
              size="small"
              @click="handleEdit(scope.row)"
              >{{ scope.row.canEdit ? "完成" : "编辑" }}</el-button
            >
            <el-button type="danger" size="small" @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="新加分类" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="食物类别" :label-width="formLabelWidth">
      <el-input v-model="form.cateName" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="是否启用" :label-width="formLabelWidth">
      <el-radio v-model="form.state" label="1">是</el-radio>
      <el-radio v-model="form.state" label="0">否</el-radio>
    </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addsortfood">确 定</el-button>
  </div>
</el-dialog>
  </el-card>
  
</template>

<script>
import { getGoodsList,updateGood,delGoodsCate,goodsaddsort  } from "@/api/goods";
export default {
  name: "GoodsCate",
  data() {
    return {
      total: 5,
      currentPage: 1,
      pageSize: 5,
      tableData: [],
      form:{
        state:"1",
        cateName:""

      },
      dialogFormVisible:false,
      formLabelWidth:''
    };
  },
  methods: {
   async addsortfood(){
    console.log(this.form)
    let resdata=JSON.parse(JSON.stringify(this.form))
     let addresults = await goodsaddsort(resdata)
     console.log(addresults)
     this.dialogFormVisible=false
     this.getData();
     this.form={
      state:"1",
        cateName:""
     }
    },
    addgoodsort(){
    this.dialogFormVisible=true

    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    async handleEdit(a) {
      a.canEdit=!a.canEdit
      // 下面代表点击的是完成
      if(!a.canEdit){
        // 点击完成后需要发请求 修改数据库数据
         let res = await updateGood({
           id:a.id,
           cateName:a.cateName,
           state:a.state?1:0
         })
         console.log(a);
      }
      
    },
  async  handleDel(b) {
      let res = await delGoodsCate({id:b.id})
    let {code} = res.data
    if(code===0){
      this.getData()
    }
   
    },
    async getData() {
      let res = await getGoodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });

      let { total, data } = res.data;

      //  处理数据的格式
      data.forEach((v) => {
        v.state = v.state == 1 ? true : false;
        // 每行都有  独立的
        v.canEdit = false;
      });

      this.total = total;
      this.tableData = data;
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.header {
  display: flex;
  justify-content: space-between;
}
</style>
